package com.ellricky.baselib.ui

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.view.View
import android.widget.AdapterView
import com.andr.common.http.callback.BaseCallback
import com.andr.common.ui.adapter.BaseRecyclerAdapter
import com.andr.common.ui.mvp.BasePresenter
import com.andr.common.util.ToastUtil
import com.ellricky.baselib.BaseConstant
import com.ellricky.baselib.adapter.SelectIntroduceAdapter
import com.ellricky.baselib.cache.LocalCacheUtil
import com.ellricky.baselib.entity.new.Introduce
import com.ellricky.baselib.ui.base.BaseRefreshFragment
import com.ellricky.baselib.util.NewEBaseCallback
import com.ellricky.baselib.util.NewEListCallback

class SelectIntroduceFragment : BaseRefreshFragment<Void, BasePresenter<Void>, Introduce>() {

    private val type: String by lazy {
        arguments?.getString(BaseConstant.KEY_PAGE_TYPE, "") ?: ""
    }

    override fun getResponseCallback(): BaseCallback? {
        return null
    }

    override fun getRvAdapter(): BaseRecyclerAdapter<Introduce> {
        return SelectIntroduceAdapter()
    }

    override fun getUrlPath(): String {
        return ""
    }

    override fun initUIView(view: View?) {
        super.initUIView(view)
        loadDataOnInit()
        hideToolBar()
    }

    override fun initParamMap() {
        super.initParamMap()
        paramMap["action"] = getAction()
        paramMap["school_id"] = LocalCacheUtil.getSchoolId()
        paramMap["sort_age"] = "0"
        paramMap["sort_birthday"] = "0"
        paramMap["sort_coursenum"] = "0"
        paramMap["eid"] = LocalCacheUtil.getUid()
    }

    override fun getResponseCallback2(): NewEBaseCallback {
        return object : NewEListCallback<Introduce>(requireActivity(), false) {
            override fun onSuccess(data: MutableList<Introduce>?, msg: String?) {
                dataSource = data
                rvView.onLoadFinish(dataSource, refresh)
            }
        }
    }

    companion object {
        fun newInstance(type: String): SelectIntroduceFragment {
            val fragment = SelectIntroduceFragment()
            val bundle = Bundle()
            bundle.putString(BaseConstant.KEY_PAGE_TYPE, type)
            fragment.arguments = bundle
            return fragment
        }
    }

    override fun onItemClick(adapterView: AdapterView<*>?, view: View?, i: Int, l: Long) {
        super.onItemClick(adapterView, view, i, l)
        val id = adapter[i].member_clue_id
        val name = adapter[i].member_name
        ToastUtil.show(mContext, "已选择${name}")
        activity?.setResult(Activity.RESULT_OK, Intent().apply {
            putExtra(BaseConstant.KEY_ID, id)
            putExtra(BaseConstant.KEY_NAME, name)
        })
        activity?.finish()
    }

    private fun getAction(): String {
        return if (type == BaseConstant.STU_TYPE_VIP_NEW) {
            "saleMemberList"
        } else {
            "saleTrialMemberList"
        }
    }
}